package com.zzyl.nursing.service.impl;

import java.util.*;

import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人家属Service业务层处理
 * 
 * @author dcl
 * @date 2025-05-23
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 查询老人家属
     * 
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     * 
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember)
    {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember)
    {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember)
    {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     * 
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     * 
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private WechatService wechatService;
    @Autowired
    private TokenService tokenService;

    private static  List<String> NICK_NAME_PREFIX_LIST = Arrays.asList("钱途无量",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝");
    @Override
    public LoginVo login(UserLoginDto dto) {
        //1. 获取用户openId
        String openid=wechatService.getOpenId(dto.getCode());
        if(StringUtils.isBlank(openid)){
            throw new BaseException("openid生成失败");
        }

        //2. 判断用户，不存在则创建
        FamilyMember member = this.lambdaQuery().eq(FamilyMember::getOpenId, openid).one();
        if(member==null){
            member=FamilyMember.builder().openId(openid).build();
            Collections.shuffle(NICK_NAME_PREFIX_LIST);
            member.setName(NICK_NAME_PREFIX_LIST.get(0)+ RandomStringUtils.randomNumeric(3));
            this.save(member);
        }

        //3. 判断手机号，不存在则根据openId获取用户手机号
        if(StringUtils.isBlank(member.getPhone())){
            //4. 获取到手机号，就更新用户信息
            String phone = wechatService.getPhone(dto.getPhoneCode());
            if(StringUtils.isNotBlank(phone)){
                this.lambdaUpdate().eq(FamilyMember::getId,member.getId()).set(FamilyMember::getPhone,phone).update();

            }
        }

        //5. 生成令牌
        Map<String,Object> map = new HashMap<>();
        map.put("id",member.getId());
        map.put("nickName",member.getName());
        String token = tokenService.createToken(map);

        //6. 返回数据

        LoginVo build = LoginVo.builder().nickName(member.getName()).token(token).build();
        return build;
    }
}
